﻿#region Usings

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using Box2DX.Common;
using Box2DX.Dynamics;
using Color=Box2DX.Dynamics.Color;

#endregion

namespace SilverBox.Physics
{
    public class DebugDrawer : DebugDraw
    {
        private Canvas canvas;
        private SimulatorContext context;
        private List<FrameworkElement> elements = new List<FrameworkElement>();

        public DebugDrawer(Canvas canvas, SimulatorContext context)
        {
            this.canvas = canvas;
            this.context = context;

            Flags = DrawFlags.Shape;
        }


        public override void DrawPolygon(Vector2[] vertices, int vertexCount, Color color)
        {
            if (vertexCount > 2)
            {
                var figure = new PathFigure();
                figure.StartPoint = context.WorldToScreen(vertices[0]);

                for (int i = 1; i < vertexCount; ++i)
                {
                    figure.Segments.Add(new LineSegment()
                                            {
                                                Point = context.WorldToScreen(vertices[i]),
                                            });
                }

                var geometry = new PathGeometry();
                geometry.Figures.Add(figure);

                var path = new Path();
                path.Data = geometry;

                path.StrokeThickness = 1;
                path.Stroke = new SolidColorBrush(color.ToPlatformColor());

                elements.Add(path);
                canvas.Children.Add(path);
            }
        }

        public override void DrawSolidPolygon(Vector2[] vertices, int vertexCount, Color color)
        {
            if (vertexCount > 2)
            {
                var figure = new PathFigure();
                figure.StartPoint = context.WorldToScreen(vertices[0]);

                for (int i = 1; i < vertexCount; ++i)
                {
                    figure.Segments.Add(new LineSegment()
                                            {
                                                Point = context.WorldToScreen(vertices[i]),
                                            });
                }

                var geometry = new PathGeometry();
                geometry.Figures.Add(figure);

                var path = new Path();
                path.Data = geometry;

                path.Fill = new SolidColorBrush(color.ToPlatformColor());

                elements.Add(path);
                canvas.Children.Add(path);
            }
        }

        public override void DrawCircle(Vector2 center, float radius, Color color)
        {
            var ellipse = new Ellipse();
            Point size = context.WorldToScreen(new Vector2(radius, radius));

            ellipse.Width = size.X*2;
            ellipse.Height = size.Y*2;

            Point centerPoint = context.WorldToScreen(center);
            Canvas.SetLeft(ellipse, centerPoint.X - ellipse.Width/2);
            Canvas.SetTop(ellipse, centerPoint.Y - ellipse.Height/2);

            ellipse.StrokeThickness = 1;
            ellipse.Stroke = new SolidColorBrush(color.ToPlatformColor());

            elements.Add(ellipse);
            canvas.Children.Add(ellipse);
        }

        public override void DrawSolidCircle(Vector2 center, float radius, Vector2 axis, Color color)
        {
            var ellipse = new Ellipse();
            Point size = context.WorldToScreen(new Vector2(radius, radius));

            ellipse.Width = size.X*2;
            ellipse.Height = size.Y*2;

            Point centerPoint = context.WorldToScreen(center);
            Canvas.SetLeft(ellipse, centerPoint.X - ellipse.Width/2);
            Canvas.SetTop(ellipse, centerPoint.Y - ellipse.Height/2);

            ellipse.Fill = new SolidColorBrush(color.ToPlatformColor());

            elements.Add(ellipse);
            canvas.Children.Add(ellipse);
        }

        public override void DrawSegment(Vector2 p1, Vector2 p2, Color color)
        {
            throw new NotImplementedException();
        }

        public override void DrawXForm(XForm xf)
        {
            throw new NotImplementedException();
        }
    }
}